<template>
  <div class="beam-layout-container">
    <!-- 顶部查询条件 -->
    <div class="screen-conditions-container">
      <ScreenConditions :isDataType="false">
        <template slot="conditions">
          <!-- 所属桥梁 -->
          <bjSelect
            :allowClear="true"
            :loading="bridgeNameLoading"
            placeholder="请选择所属桥梁"
            :replaceFields="bridgeNameReplaceFields"
            :dataResource="bridgeNameSource"
            v-model="bridgeName"
            @change="conditionChange($event, 'bridgeName')"
          >
          </bjSelect>
          <!-- 幅别 -->
          <bjSelect
            :allowClear="true"
            placeholder="请选择幅别"
            :dataResource="amClassSource"
            v-model="amClass"
            @change="conditionChange($event, 'amClass')"
          >
          </bjSelect>
          <!-- 梁类型 -->
          <bjSelect
            :allowClear="true"
            :loading="beamTypeLoading"
            placeholder="请选择梁类型"
            :replaceFields="beamTypeReplaceFields"
            :dataResource="beamTypeSource"
            v-model="beamType"
            @change="conditionChange($event, 'beamType')"
          >
          </bjSelect>
          <!-- 梁号 -->
          <a-input
            placeholder="请输入梁号"
            v-model="componentParts"
            allowClear
          />
          <!-- 状态 -->
          <bjSelect
            :allowClear="true"
            placeholder="请选择状态"
            :dataResource="stateSource"
            v-model="state"
            @change="conditionChange($event, 'state')"
          >
          </bjSelect>
          <!-- 时间 -->
          <bjRangeCalendar
            :format="dateFormat"
            v-model="dateRange"
            :show-time="{ format: dateFormat }"
            @ok="calendarOk"
            @change="calendarChange"
          ></bjRangeCalendar>
        </template>
        <template slot="operate">
          <a-button type="primary" @click="search()"> 查询 </a-button>
        </template>
      </ScreenConditions>
    </div>
    <div class="beam-table-container">
      <div class="table-header" header="钢筋加工数据"></div>
      <div class="main" v-loading="loading">
        <vxe-table
          ref="xTable"
          height="99%"
          class="scrollbarStyle"
          align="center"
          show-overflow
          show-header-overflow
          :data="tableData"
          :column-config="{ resizable: true }"
        >
          <vxe-column type="seq" width="80" title="序号"></vxe-column>
          <vxe-column
            show-overflow
            :key="index"
            :title="item.title"
            :field="item.key"
            :min-width="item.width"
            v-for="(item, index) in columns"
          >
            <template #default="{ row }">
              <div
                v-if="item.key == 'completeStatus'"
                :class="[checkStatusClass(row), 'status-text']"
              >
                {{ checkStatusName(row) }}
              </div>
              <div v-else>{{ row[item.key] }}</div>
            </template>
          </vxe-column>
          <vxe-column
            field="operate"
            width="220"
            title="操作"
            fixed="right"
            align="center"
          >
            <template #default="{ row }">
              <div class="text-button-group">
                <a-button
                  type="link"
                  @click="gotoView(row, 1)"
                  v-if="row.completeStatus == 0 && row.approve"
                >
                  接单
                </a-button>
                <a-button
                  type="link"
                  @click="gotoView(row, 2)"
                  v-if="row.completeStatus == 0 && row.approve"
                >
                  退回
                </a-button>
                <a-button
                  type="link"
                  @click="gotoView(row, 3)"
                  v-if="row.completeStatus == 2 && row.approve"
                >
                  生产加工
                </a-button>
                <a-button
                  type="link"
                  @click="gotoView(row, 4)"
                  v-if="row.completeStatus == 3 && row.approve"
                >
                  钢筋质检
                </a-button>
                <a-button
                  type="link"
                  @click="gotoView(row, 5)"
                  v-if="row.completeStatus == 4 && row.approve"
                >
                  生产完成
                </a-button>
                <a-button type="link" @click="gotoView(row, 6)">
                  流程监控
                </a-button>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="pager-container">
        <vxe-pager
          background
          :current-page.sync="pager.page"
          :page-size.sync="pager.pageSize"
          :total="pager.total"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes', 'FullJump']"
          @page-change="paginationChange"
        >
          <template #left>
            <div class="vxe-pager-left-tip-container">
              第<span>1</span>到<span
                >{{ Math.ceil(pager.total / pager.pageSize) || 1 }}页 </span
              >共<span>{{ pager.total }}</span
              >条记录
            </div>
          </template>
        </vxe-pager>
      </div>
    </div>
    <viewPage
      :visible.sync="pageVisible"
      :type="operateType"
      :row="row"
      :pageType="pageType"
    ></viewPage>
  </div>
</template>

<script>
import bjSelect from "@/components/bj-select";
import bjRangeCalendar from "@/components/bj-range-calendar";
import viewPage from "./components/view.vue";
import { getRebarProcessPage } from "@/api/wisdom-beam/collect-app.js";
import { getBridgeList } from "@/api/wisdom-beam/beamInfoConfig";
import { getDickeyList } from "@/api/common";
export default {
  components: {
    bjSelect,
    bjRangeCalendar,
    viewPage,
  },
  data() {
    return {
      operateType: 1,
      pageVisible: false,
      row: {},
      pageType: "",
      columns: [
        {
          title: "梁号",
          key: "componentParts",
        },
        {
          title: "桥梁名称",
          key: "bridgeName",
        },
        {
          title: "幅别",
          key: "amClass",
        },
        {
          title: "跨",
          key: "span",
        },
        {
          title: "梁类型",
          key: "beamTypeDesc",
        },
        {
          title: "下单人员",
          key: "createrName",
        },
        {
          title: "下单时间",
          key: "createTime",
        },
        {
          title: "计划完成时间",
          key: "planCompleteTime",
        },
        {
          title: "状态",
          key: "completeStatus",
        },
      ],
      loading: false,
      tableData: [],
      pager: {
        page: 1,
        pageSize: 10,
        total: 0,
      },
      //所属桥梁
      bridgeName: undefined,
      bridgeNameSource: [],
      bridgeNameLoading: false,
      bridgeNameReplaceFields: { key: "key", label: "value" },
      //所属幅别
      amClass: undefined,
      amClassSource: [
        {
          key: "左幅",
          value: "左幅",
        },
        {
          key: "右幅",
          value: "右幅",
        },
      ],
      //所属梁类型
      beamTypeLoading: false,
      beamType: undefined,
      beamTypeSource: [],
      beamTypeReplaceFields: { key: "dicValue", label: "dicDesc" },
      //梁号
      componentParts: "",
      //状态
      state: undefined,
      stateSource: [
        {
          key: "0",
          value: "待接单",
        },
        {
          key: "1",
          value: "已退回",
        },
        {
          key: "2",
          value: "待生产",
        },
        {
          key: "3",
          value: "待质检",
        },
        {
          key: "4",
          value: "已完成",
        },
        // {
        //   key: "5",
        //   value: "已完成",
        // },
      ],
      //时间
      dateFormat: "YYYY-MM-DD HH:mm:ss",
      dateRange: [],
    };
  },
  mounted() {
    let { componentParts = "", type = "" } = this.$route.query;
    this.componentParts = componentParts;
    this.pageType = type;
    //
    this.getBridgeList();
    this.getBeamType();
    this.getTableData();
  },
  methods: {
    //查询桥梁列表
    getBridgeList() {
      this.bridgeNameLoading = true;
      return new Promise((resolve, reject) => {
        getBridgeList({})
          .then((res) => {
            let { data } = res;
            let list = data?.result || [];
            this.bridgeNameSource = list.map((v) => {
              return { key: v, value: v };
            });
            resolve();
          })
          .catch((_) => {
            this.bridgeNameSource = [];
            reject();
          })
          .finally((_) => {
            this.bridgeNameLoading = false;
          });
      });
    },
    //查询梁类型
    getBeamType() {
      this.beamTypeLoading = true;
      return new Promise((resolve, reject) => {
        getDickeyList({ dictKey: "TASK_MANAGER_TYPE" })
          .then((res) => {
            let { data } = res;
            this.beamTypeSource = data?.result ?? [];
            resolve();
          })
          .catch((_) => {
            this.beamTypeSource = [];
            reject();
          })
          .finally((_) => {
            this.beamTypeLoading = false;
          });
      });
    },
    gotoView(row, type) {
      this.row = row;
      this.operateType = type;
      this.pageVisible = true;
    },

    search() {
      this.pager.page = 1;
      this.getTableData();
    },

    //监听筛选条件切换
    conditionChange(ev, keyName) {
      this[keyName] = ev;
      this.search();
    },

    //查询列表
    getTableData() {
      this.loading = true;
      let { page, pageSize } = this.pager;
      let { componentParts, bridgeName, amClass, beamType, state, dateRange } =
        this;
      return new Promise((resolve, reject) => {
        getRebarProcessPage({
          page,
          pageSize,
          bridgeName: bridgeName?.key,
          componentParts: componentParts,
          type: beamType?.key,
          amClass: amClass?.key,
          status: state?.key,
          startTime: dateRange?.[0],
          endTime: dateRange?.[1],
        })
          .then((res) => {
            let {
              data: { result },
            } = res;
            this.tableData = result?.rows || [];
            this.pager.total = result?.total || 0;
            resolve();
          })
          .catch((_) => {
            this.tableData = [];
            this.pager.total = 0;
            reject();
          })
          .finally((_) => {
            this.loading = false;
          });
      });
    },

    //监听日期选择
    calendarOk(dates = []) {
      this.dateRange = dates;
      this.search();
    },

    calendarChange(dates = []) {
      this.dateRange = dates;
      if (this.dateRange.length == 0) {
        this.search();
      }
    },

    //监听分页切换
    paginationChange({ currentPage, pageSize }) {
      this.pager.page = currentPage;
      this.pager.pageSize = pageSize;
      this.getTableData();
    },

    //校验状态className
    checkStatusClass(row) {
      let { completeStatus } = row;
      switch (completeStatus) {
        case 0:
          return "pending";

        case 1:
          return "error";

        case 2:
          return "warn";

        case 3:
          return "produce";

        case 4:
          return "normal";

        // case 5:
        //   return "approv";

        default:
          return "cancel";
      }
    },

    //校验状态名称
    checkStatusName(row) {
      let { completeStatus } = row;
      switch (completeStatus) {
        case 0:
          return "待接单";

        case 1:
          return "已退回";

        case 2:
          return "待生产";

        case 3:
          return "待质检";

        case 4:
          return "已完成";

        // case 5:
        //   return "已完成";

        default:
          return "-";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.table-header {
  padding: 28px 20px !important;
}
.status-text {
  border-radius: 4px;
  color: #fff;
  .flex();
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  font-size: 12px;
  width: 80px;
  &.pending {
    background: #0098ff;
  }
  &.warn {
    background: #ff993f;
  }
  &.produce {
    background: #f33ecb;
  }
  &.approv {
    background: #ff7113;
  }
  &.error {
    background: #ff3f4a;
  }
  &.normal {
    background: #4bc964;
  }
  &.cancel {
    background: #f1f2f7;
    color: #aab0bc;
  }
}
</style>
